import { login, logout, getInfo, userInfo } from '@/api/user'
import { getToken, setToken, setRefreshToken, setTime, removeToken, removeRefToken, removeTime, setUser } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  userLocation: '',
  refToken: '',
  time: '',
  name: '',
  avatar: '',
  roles: [],
  menu: []
}

const mutations = {
  SET_REFTOKEN: (state, token) => {
    state.refToken = token
  },
  SET_TIME: (state, time) => {
    state.time = time
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_MENU: (state, menu) => {
    state.menu = menu
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, code } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password, code: code }).then(response => {
        console.log(response)
        const expiredTime = new Date().getTime() + response.expires_in * 1000 * 1000 / 1000
        commit('SET_TOKEN', response.access_token)
        commit('SET_TIME', expiredTime)
        commit('SET_REFTOKEN', response.refresh_token)
        setToken(response.access_token)
        setUser(username)
        setRefreshToken(response.refresh_token)
        setTime(expiredTime)

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  getUserInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      userInfo().then(response => {
        console.log(response)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      // const data = {}
      // data.roles = ['admin']
      // data.name = 'ceshi'
      // data.avatar = '13'
      // const { roles, name, avatar } = data

      // commit('SET_ROLES', roles)

      getInfo().then(response => {
        console.log(response.menuList)
        commit('SET_ROLES', response.menuList)
        //  commit('SET_MENU',response);
        commit('SET_NAME', response.username)
        commit('SET_AVATAR', '/images/head.png')
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_REFTOKEN', '')
        commit('SET_TIME', '')
        commit('SET_ROLES', [])
        commit('SET_NAME', '')
        removeToken()
        removeRefToken()
        removeTime()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

